﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class AdminAdvertisementController : AdminController
    {
        //
        // GET: /AdminAdvertisement/

        public ActionResult Index()
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    AdvertHelper advertHelper = new AdvertHelper();
                    ViewData["ListAdvert"] = advertHelper.getAllListAdvertisement(pageNum, Config.ITEMS_PER_ADMINPAGE);
                    return View("~/Views/Admin/Advertisement.aspx");
                }
            }
            return HomeAdminPage();
        }

        [HttpGet]
        public ActionResult DeleteAdvert(int advertId)
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int currPage = 0;
                    currPage = int.Parse(Request.QueryString["p"]);
                    if (currPage == 0)
                        currPage = 1;
                    AdvertHelper advertHelper = new AdvertHelper();
                    Advertisment advert = advertHelper.getAdvertById(advertId);
                    advertHelper.Delete(advert);
                    advertHelper.Save();
                    return RedirectToAction("Index", new { p = currPage });
                }
            }
            return HomeAdminPage();
        }

    }
}
